package com.primb.androidlibs.net.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by wumengmeng on 2016/8/9/0009.
 */
public class HttpConfig {
    private Context context;
    private static HttpConfig mInstance = null;
    private final String SP_NAME = "http_config";
    private final String SP_KEY_TOKEN = "user.token";
    private final String SP_LOGIN_TOKEN = "login.token";
    private final String SP_DEVICE_MODEL = "device_model";
    private final String SP_DEVICE_ID = "device_num";
    private final String SP_USER_ID = "user_id";
    private SharedPreferences sp;
    private SharedPreferences.Editor mEditor;

    private HttpConfig(Context context) {
        this.context = context;
        sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        mEditor = sp.edit();
    }

    public static HttpConfig newInstance(Context context) {
        if (mInstance == null) {
            mInstance = new HttpConfig(context);
        }
        return mInstance;
    }

    public void setAccessToken(String token) {
        mEditor.putString(SP_KEY_TOKEN, token).commit();
    }

    public String getAccessToken() {
        String token = sp.getString(SP_KEY_TOKEN, "soask_5366.com");
        return TextUtils.isEmpty(token) ? "soask_5366.com" : token;
    }

    public String getLoginToken() {
        String token = sp.getString(SP_LOGIN_TOKEN, "soask_5366.com");
        return token;
    }

    public void setLoginToken(String token) {
        mEditor.putString(SP_LOGIN_TOKEN, token).commit();
    }

    public void setDeviceModel(String model) {
        mEditor.putString(SP_DEVICE_MODEL, model).commit();
    }

    public String getDeviceModel() {
        return sp.getString(SP_DEVICE_MODEL, "");
    }

    public void setDeviceId(String deviceId) {
        mEditor.putString(SP_DEVICE_ID, deviceId).commit();
    }

    public String getDeviceId() {
        return sp.getString(SP_DEVICE_ID, "");
    }

    public void setUserId(String userId) {
        mEditor.putString(SP_USER_ID, userId).commit();
    }

    public String getUserId() {
        String userId = sp.getString(SP_USER_ID, "-1");
        return TextUtils.isEmpty(userId) ? "-1" : userId;
    }

}
